package com.my.java_web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.my.java_web.dao.UserDao;
import com.my.java_web.dao.impl.UserDaoImpl;
import com.my.java_web.domain.User;

@WebServlet("/users/list")
public class UserServlet extends HttpServlet{
	private UserDao userService = new UserDaoImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		PrintWriter print =  resp.getWriter();
//		print.write("hello servlet");
		resp.setCharacterEncoding("utf-8");
		PrintWriter print = resp.getWriter();
		List<User> users = userService.query(new User());
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("count", users.size());
		jsonObject.put("data", users);
		resp.setContentType("application/json,charset=utf-8");
		print.write(jsonObject.toJSONString());
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}

}
